package example;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestEatGif extends JFrame {
	private static final long serialVersionUID = 1L;
	Image img[] = new Image[14];
	int index = 0;

	public TestEatGif() {
		for (int i = 0; i < 14; i++) {
			img[i] = Toolkit.getDefaultToolkit().createImage(
					"img/eat/" + (i + 1) + ".gif");
		}
		add(new EatPanel());
		MyThread th1 = new MyThread();
		th1.start();
	}

	public static void main(String[] args) {
		TestEatGif frame = new TestEatGif();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		frame.setVisible(true);
	}

	class EatPanel extends JPanel {
		private static final long serialVersionUID = 1L;

		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(img[index], 0, 0, this);
		}
	}

	class MyThread extends Thread {

		public void run() {
			while (true) {
				try {
					sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if (index == 13)
					index = 0;
				else
					index++;
				repaint();
			}
		}
	}
}
